//工程名称
var contentPath="/MedicalSystem";

document.write('<link rel="stylesheet" href="' + contentPath + '/js/ztree/css/zTreeStyle/zTreeStyle.css" type="text/css"></link>');
document.write('<script type="text/javascript" src="' + contentPath + '/js/ztree/js/jquery.ztree.core-3.5.min.js"></script>');
document.write('<script type="text/javascript" src="' + contentPath + '/js/ztree/js/jquery.ztree.excheck-3.5.min.js"></script>');
document.write('<script type="text/javascript" src="' + contentPath + '/js/ztree/js/jquery.ztree.exedit-3.5.min.js"></script>');
//默认的点击事件

function _onNodeClick(event,treeId,treeNode)
{
	
}

function addNode(zTreeObj,treeNode,isSelect)
{
	
	var ztree = typeof(zTreeObj) == 'string' ? $.fn.zTree.getZTreeObj(zTreeObj) : zTreeObj;
	isSelect = isSelect ? isSelect : true;
	if(typeof(ztree) != 'object' || !treeNode)
	{
		return;
	}
	var parentNode = ztree.getNodeByParam("id",treeNode.pId);
	if(parentNode)
	{
		var nodes = ztree.addNodes(parentNode,[{id:treeNode.id, pId:treeNode.pId, name:treeNode.name}]);
		if(isSelect && nodes && nodes.length == 1)
		{
			ztree.selectNode(nodes[0]);
		}
	}
}

function edit(zTreeObj,treeNode,isSelect)
{
	var ztree = typeof(zTreeObj) == 'string' ? $.fn.zTree.getZTreeObj(zTreeObj) : zTreeObj;
	isSelect = isSelect ? isSelect : true;
	if(typeof(ztree) != 'object' || !treeNode)
	{
		return;
	}
	var node = ztree.getNodeByParam("id",treeNode.id);
	if(node)
	{
		node.name = treeNode.name;
		ztree.updateNode(node);
		ztree.selectNode(node);
	}
}

function remove(zTreeObj,treeNode)
{
	var ztree = typeof(zTreeObj) == 'string' ? $.fn.zTree.getZTreeObj(zTreeObj) : zTreeObj;
	if(typeof(ztree) != 'object' || !treeNode)
	{
		return;
	}
	var node = ztree.getNodeByParam("id",treeNode.id);
	if(node)
	{
		//删除所有子节点
		ztree.removeChildNodes(node);
		//选中它的父节点
		ztree.selectNode(node.getParentNode());
		//再删除自己
		ztree.removeNode(node);
	}
}

function refreshTree(zTreeObj,nodeId)
{
	var ztree = typeof(zTreeObj) == 'string' ? $.fn.zTree.getZTreeObj(zTreeObj) : zTreeObj;
	if(typeof(ztree) != 'object')
	{
		return;
	}
	ztree.refresh();
	if(nodeId)
	{
		ztree.selectNode(ztree.getNodeByTId(nodeId));
	}
}

function createSimpleTree(option)
{
	
	var setting = {
			async:{
				//使用异步加载模式
				enable: true,
				dateType:"json",
				url:option.url,
				autoParam:["id=parentId"],
				otherParam:[]
			},
			view: {
				dblClickExpand: true,	//双击自动展开
				showLine: true,			//是否显示连接线
				selectedMulti: false,	//是否支持多选
				expandSpeed: ($.browser.msie && parseInt($.browser.version)<=6)?"":"fast"
			},

			data: {
				simpleData: {
					enable:true,
					idKey: "id",
					pIdKey: "pId",
					rootPId: ""
				}
			},
			
			check: {
				enable: (option.chkStyle ? true : false)
			},
			
			callback:{
				onClick: option.onNodeClick ? option.onNodeClick : _onNodeClick,
				onAsyncSuccess: function(event,treeId,treeNode,msg){
					var zTree = $.fn.zTree.getZTreeObj(option.zTreeId);
					
					if(option.chkStyle && ($.trim(option.chkStyle) == "checkbox" || $.trim(option.chkStyle) == "radio"))
					{
						zTree.setting.check.chkboxType = { "Y" : "ps", "N" : "ps" };
						zTree.setting.check.chkStyle = option.chkStyle;
					}
					zTree.expandAll(option.expandAll || option.expandAll == false ? option.expandAll : true);
				}
			}
	};
	
	return $.fn.zTree.init($("#"+option.zTreeId), setting);
}
